package com.mp.approvalreq.entity;


import com.plusmoney.exception.RpcServerLogicalException;
import com.plusmoney.util.ValidateUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * 审批流类型
 */
public class RequestFlowType {

	// 条件审批流
	public static final String CONDITION = "Condition";

	// 灵活审批流
	public static final String ALTERNATIVE = "Alternative";

	// 默认审批流
	public static final String DEFAULT = "default";

	// 自定义审批流(直接上级):  直接上级,xx,xx
	public static final String SP_DEFINE = "SP";

	// 自定义审批流(直接上级、间接上级):  直接上级,间接上级,xx
	public static final String SP_DM_DEFINE = "SP,DM";

	// 注: 不存在没有SP,就有DM的情况

	// 全部是指定人审批流:xx,xx,xx
	public static final String ALL_NUMBER = "xx,xx,xx";


	public static String getFlowType(String flowStr) {
		if (StringUtils.isEmpty(flowStr)) {
			throw new RpcServerLogicalException(1, "请设置审批流程");
		}

		if (Objects.equals(CONDITION, flowStr)) {
			return CONDITION;
		}

		if (Objects.equals(ALTERNATIVE, flowStr)) {
			return ALTERNATIVE;
		}

		if (Objects.equals(DEFAULT, flowStr)) {
			return DEFAULT;
		}

		// 先判断: SP,DM
		if (flowStr.contains(SP_DM_DEFINE)) {
			return SP_DM_DEFINE;
		}

		if (flowStr.contains(SP_DEFINE)) {
			return SP_DEFINE;
		}

		for (String flow : flowStr.split(",")) {
			if (flow.contains("||") || flow.contains("&&")) {
				continue;
			}
			if (!ValidateUtil.isInteger(flow)) {
				throw new RpcServerLogicalException(1, "错误的审批流");
			}
		}
		return ALL_NUMBER;
	}

}
